<?php

function GoogleMap_AdminHeader($target) {
	global $suri, $pluginURL, $blogURL, $serviceURL, $configVal, $blog, $service;
	if (!isset($blog['blogLanguage'])) {
		$blog['blogLanguage'] = $service['language'];
	}
	if ($suri['directive'] == '/owner/entry/post' || $suri['directive'] == '/owner/entry/edit') {
		$target .= "<script type=\"text/javascript\">
		//<![CDATA[
			var pluginURL = '".$pluginURL."';
			var blogURL = '".$blogURL."';		
			function GMapTool_Insert() {
				window.open(blogURL + '/plugin/GMapCustomInsert/', 'GMapTool_Insert', 'menubar=no,toolbar=no,width=560,height=480,scrollbars=no,resizable=no');
			}
		//]]>
		</script>";
	}
	return $target;
}

function GoogleMap_AddToolbox($target) {
	global $pluginURL, $blog, $service;
	if (!isset($blog['blogLanguage'])) $blog['blogLanguage'] = $service['language'];
	switch($blog['blogLanguage']) {
		case "zh-TW":
			$imgdesc ='插入 Google 地圖';
		break;
		case "zh-CN":
			$imgdesc ='插入 Google 地图';
		break;
		default:
			$imgdesc = '구글맵 추가하기';
	}
	if(!empty($languageFile)) { include ($languageFile); }	
	$target .= "<img src=\"$pluginURL/images/gmap_toolbar.gif\" border=\"0\" alt=\"$imgdesc\" onclick=\"GMapTool_Insert();\" style=\"cursor:pointer\" />\n";
	return $target;
}

function googleMapFlashJavascript($target) {
	ob_start();
?>
	<script type="text/javascript">
	//<![CDDA[
	var mapdata = new Array();
	function getMarkersFromJS(mapid, offset) {
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		var flashCtrl = (isIE) ? window[mapid] : document[mapid];
		flashCtrl.SendMarkers(mapdata[offset]);
	}
	//]]>
	</script>
<?php	
	$target .= ob_get_contents();
	ob_end_clean();
	
	return $target;
}

function GoogleMap_View($target, $mother) {
	global $configVal, $pluginURL;
	requireComponent('Textcube.Function.Setting');
	requireComponent('Textcube.Function.Misc');
	$config = Setting::fetchConfigVal($configVal);
	
	$matches = array();
	$offsetid = 0;
	$offset = 0;
	while (preg_match('/\[##_GoogleMap\|(([^|]+)\|)?_##\]/', $target, $matches, PREG_OFFSET_CAPTURE, $offset) > 0) {
		// SUGGUEST: [##_GoogleMap|{JSON_REPRESENTATION_OF_PARAMETERS_WITHOUT_NEWLINES}|_##]
		$id = 'GMapContainer'.$mother.$offsetid;
		$mapResult = str_replace("\n",'', nl2br($matches[2][0]));
		ob_start();
?>
		<script type="text/javascript">
		//<![CDDA[
		mapdata[<?php echo $mother.$offsetid;?>] = <?php echo $mapResult;?>;
		//]]>
		</script>
		<div id="<?php echo $id;?>" style="border: 1px solid #666; width:550px; height:auto;">
			<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="375" id="<?php echo $id;?>" align="middle">
			<param name="allowScriptAccess" value="sameDomain" />
			<param name="allowFullScreen" value="false" />
			<param name="movie" value="<?php echo $pluginURL;?>/googlemapclient.swf" />
			<param name="quality" value="high" />
			<param name="bgcolor" value="#ffffff" />
			<param name="flashVars" value="apiKey=<?php echo $config['apiKey'];?>&lat=<?php echo $config['latitude'];?>&lng=<?php echo $config['longitude'];?>&zoom=8&mapid=<?php echo $id;?>&offsetid=<?php echo $mother.$offsetid;?>" />
			<embed src="<?php echo $pluginURL;?>/googlemapclient.swf" flashVars="apiKey=<?php echo $config['apiKey'];?>&lat=<?php echo $config['latitude'];?>&lng=<?php echo $config['longitude'];?>&zoom=8&mapid=<?php echo $id;?>&offsetid=<?php echo $mother.$offsetid;?>" quality="high" bgcolor="#ffffff" width="550" height="375" name="<?php echo $id;?>" id="<?php echo $id;?>" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
			</object>		
		</div>		
<?php
		$output = ob_get_contents();
		ob_end_clean();
		$target = substr_replace($target, $output, $matches[0][1], strlen($matches[0][0]));
		$offset += $matches[0][1] + strlen($output);
		$offsetid++;
		
	}
	return $target;
}

function GoogleMap_ConfigHandler($data) {
	global $blog, $service;
	if (!isset($blog['blogLanguage'])) $blog['blogLanguage'] = $service['language'];
	switch($blog['blogLanguage']) {
		case "zh-TW":
			$err ='經緯度設定錯誤。';
		break;
		case "zh-CN":
			$err ='经纬度设定错误。';
		break;
		default:
			$err = '위도 또는 경도의 값이 올바르지 않습니다.';
	}
	requireComponent('Textcube.Function.Setting');
	$config = Setting::fetchConfigVal($data);
	if (!is_numeric($config['latitude']) || !is_numeric($config['longitude']) ||
		$config['latitude'] < -90 || $config['latitude'] > 90 || $config['longitude'] < -180 || $config['longitude'] > 180)
		return $err;
	return true;
}

function GoogleMapUI_Insert() {
	global $configVal, $pluginURL, $blog, $service;
	requireComponent('Textcube.Function.Misc');
	$config = Setting::fetchConfigVal($configVal);
	if (!isset($blog['blogLanguage'])) $blog['blogLanguage'] = $service['language'];
	$gtext = array();
	$gtext[0] ='Google Map Flash';
	$gtext[1] = 'Insert';
	$gtext[2] = 'Cancel';

	_GMap_printHeaderForUI($gtext[0]);
?>
	<div style="text-align:center;">
		<div id="GoogleMapPreview" style="width:<?php echo $default_width;?>px; height:<?php echo $default_height;?>px; margin:0 auto;">
			<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="375" id="googlemap" align="middle">
			<param name="allowScriptAccess" value="sameDomain" />
			<param name="allowFullScreen" value="false" />
			<param name="movie" value="<?php echo $pluginURL;?>/googlemap.swf" />
			<param name="quality" value="high" />
			<param name="bgcolor" value="#ffffff" />
			<param name="flashVars" value="apiKey=<?php echo $config['apiKey'];?>&lat=<?php echo $config['latitude'];?>&lng=<?php echo $config['longitude'];?>&zoom=8" />
			<embed src="<?php echo $pluginURL;?>/googlemap.swf" flashVars="apiKey=<?php echo $config['apiKey'];?>&lat=<?php echo $config['latitude'];?>&lng=<?php echo $config['longitude'];?>&zoom=8" quality="high" bgcolor="#ffffff" width="550" height="375" name="googlemap" id="googlemap" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
			</object>
		</div>
	</div>
	<div id="controls">
		<button onclick="getMarkersFromMap(); return false;"><?php echo $gtext[1];?></button>
		<button onclick="self.close(); return false;"><?php echo $gtext[2];?></button>
	</div>
<?php
	_GMap_printFooterForUI();
}

function _GMap_printHeaderForUI($title) {
	global $pluginURL, $blogURL, $service, $blog;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Google Map Plugin: <?php echo $title;?></title>
	<link rel="stylesheet" type="text/css" href="<?php echo $pluginURL;?>/insert.css" />
	<script type="text/javascript">
	//<![CDATA[
	var pluginURL = '<?php echo $pluginURL;?>';
	var blogURL = '<?php echo $blogURL;?>';
	function getFlashMovie(movieName) {
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		return (isIE) ? window[movieName] : document[movieName];
	}
	function getMarkersFromMap() {
		getFlashMovie("googlemap").SendMarkers();
	}
	function SendMarkersToJSON(str) {
		try{
			var editor = window.opener.editor;
			var mediaCode = str;
			editor.command("Raw", mediaCode, "");
			self.close();
		}
		catch(e) { }
	}
	function markerLimitAlert(limit) {
		alert('Markers max limit: '+parseInt(limit));
	}
	//]]>
	</script>
</head>
<body>
<div id="all-wrap">
	<h1><?php echo $title;?></h1>
	<div id="layout-body">
<?php
}

function _GMap_printFooterForUI() {
?>
	</div>
</div>
</body>
</html>
<?php
}

function _GMap_normalizeAddress($address) {
	return trim(implode(' ', explode('/', $address)));
}
/* vim: set noet ts=4 sts=4 sw=4: */
?>